//
//  CountryViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/6.
//  Copyright © 2016年 KX. All rights reserved.
#import "CollectionViewAlbum.h"
#import "FreeTravelNetwork.h"
#import "CountryViewController.h"
#import "ModelOfCountry.h"
#import "FreeTravelMacro.h"
#import "Masonry.h"
#import "CellOfCollectionViewOfHot_city.h"
#import "CellOfCollectionViewOfNew_discount.h"
#import "MapViewController.h"
#import "ModelOfCityMap.h"
#import "CityViewController.h"
#import "ModelOfCountryHot_city.h"
#import "ModelOfCountryNew_discount.h"
#import "WebViewOfCommendViewController.h"
@interface CountryViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ModelOfCountry *model;
@property (nonatomic, strong) CollectionViewAlbum *album;
@property (nonatomic, strong) UILabel *labelOfEntryCont ;
@property (nonatomic, strong) UILabel *labelOfCnname;;
@property (nonatomic, strong) UILabel *labelOfEnname;
@property (nonatomic, strong) UIButton *buttonOfGuide_num;
@property (nonatomic, strong) UILabel *labelOfCnnameButtonOfGuide_num;
@property (nonatomic, strong) UILabel *labelOfGuide_numButtonOfGuide_num;
@property (nonatomic, strong) UICollectionView *collectionViewOfHot_city;
@property (nonatomic, strong) UICollectionView *collectionViewOfNew_discount;
@end

@implementation CountryViewController
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setTranslucent:YES];
    [self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    [self createTableView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sdfasadfas"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"maplogo"] style:UIBarButtonItemStylePlain target:self action:@selector(goMap)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1150)];
    self.album = [[CollectionViewAlbum alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    
    [_headView addSubview:self.album];
    self.labelOfCnname = [[UILabel alloc]init];
    self.labelOfEnname = [[UILabel alloc]init];
    self.labelOfEntryCont = [[UILabel alloc]init];
    [self.album addSubview:self.labelOfCnname];
    [self.album addSubview:self.labelOfEntryCont];
    [self.album addSubview:self.labelOfEnname];

    [self.labelOfCnname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.album);
        make.height.mas_offset(15);
        make.left.equalTo(self.album.mas_left).mas_offset(20);
    }];
    self.labelOfCnname.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.f];
    self.labelOfCnname.textColor = [UIColor whiteColor];
    [self.labelOfEnname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelOfCnname.mas_bottom).mas_offset(10);
        make.left.equalTo(self.album.mas_left).mas_offset(20);
        make.height.mas_offset(10);
    }];
    self.labelOfEnname.font = [UIFont systemFontOfSize:12];
    self.labelOfEnname.textColor = [UIColor whiteColor];
    [self.labelOfEntryCont mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.album.mas_left).mas_offset(20);
        make.right.equalTo(self.album.mas_right).mas_offset(-20);
        make.bottom.equalTo(self.album.mas_bottom).mas_offset(-20);
        make.top.equalTo(self.labelOfEnname.mas_bottom).mas_offset(20);
    }];
    self.labelOfEntryCont.font = [UIFont systemFontOfSize:12];
    self.labelOfEntryCont.textColor = [UIColor whiteColor];
    self.labelOfEntryCont.numberOfLines  = 0;
    
    self.buttonOfGuide_num = [UIButton buttonWithType:UIButtonTypeCustom];
    [_headView addSubview:self.buttonOfGuide_num];
    [self.buttonOfGuide_num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_headView);
        make.top.equalTo(self.album.mas_bottom);
        make.height.mas_offset(50);
    }];
    self.labelOfCnnameButtonOfGuide_num = [[UILabel alloc]init];
    self.labelOfGuide_numButtonOfGuide_num = [[UILabel alloc]init];
    [self.buttonOfGuide_num addSubview:self.labelOfGuide_numButtonOfGuide_num];
    [self.buttonOfGuide_num addSubview:self.labelOfCnnameButtonOfGuide_num];
    [self.labelOfCnnameButtonOfGuide_num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.buttonOfGuide_num.mas_centerY);
        make.height.mas_offset(15);
        make.left.equalTo(self.buttonOfGuide_num.mas_left).mas_offset(20);
    }];
    
    [self.labelOfGuide_numButtonOfGuide_num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.buttonOfGuide_num.mas_centerY);
        make.height.mas_offset(15);
        make.right.equalTo(self.buttonOfGuide_num.mas_right).mas_offset(-20);
    }];
    self.labelOfGuide_numButtonOfGuide_num.font = [UIFont systemFontOfSize:12];
    self.labelOfGuide_numButtonOfGuide_num.textColor = [UIColor grayColor];
    
    UIView *grayView = [[UIView alloc]init];
    [_headView addSubview:grayView];
    grayView.backgroundColor = [UIColor grayColor];
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buttonOfGuide_num.mas_bottom);
        make.left.right.equalTo(_headView);
        make.height.mas_offset(5);
    }];
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc]init];
    self.collectionViewOfHot_city = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 255, SCREEN_WIDTH,600) collectionViewLayout:layout1];
    self.collectionViewOfHot_city.scrollEnabled = NO;
    [_headView addSubview:self.collectionViewOfHot_city];
    layout1.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout1.itemSize = CGSizeMake((SCREEN_WIDTH-30)/2, 100);
    layout1.minimumLineSpacing = 10;
    layout1.minimumInteritemSpacing = 10;
    layout1.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionViewOfHot_city.delegate= self;
    self.collectionViewOfHot_city.dataSource = self;
    [self.collectionViewOfHot_city registerClass:[CellOfCollectionViewOfHot_city class] forCellWithReuseIdentifier:@"CellOfCollectionViewOfHot_city"];
    self.collectionViewOfHot_city.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *layout2 = [[UICollectionViewFlowLayout alloc]init];
    self.collectionViewOfNew_discount = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 505, SCREEN_WIDTH, 550) collectionViewLayout:layout2];
    self.collectionViewOfNew_discount.scrollEnabled = NO;
    [_headView addSubview:self.collectionViewOfNew_discount];
    layout2.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout2.itemSize = CGSizeMake(SCREEN_WIDTH-20, 100);
    layout2.minimumLineSpacing = 10;
    layout2.minimumInteritemSpacing = 10;
    layout2.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionViewOfNew_discount.delegate= self;
    self.collectionViewOfNew_discount.dataSource = self;
    [self.collectionViewOfNew_discount registerClass:[CellOfCollectionViewOfNew_discount class] forCellWithReuseIdentifier:@"CellOfCollectionViewOfNew_discount"];
    self.collectionViewOfNew_discount.backgroundColor = [UIColor whiteColor];
    
    
    
    
    self.tableView.tableHeaderView = _headView;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collectionViewOfHot_city) {
        return self.model.data.hot_city.count;
    }else{
        return self.model.data.New_discount.count;
    }
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionViewOfHot_city) {
        CellOfCollectionViewOfHot_city *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfCollectionViewOfHot_city" forIndexPath:indexPath];
        cell.model = self.model.data.hot_city[indexPath.item];
        
        return cell;
    }else{
        CellOfCollectionViewOfNew_discount *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfCollectionViewOfNew_discount" forIndexPath:indexPath];
        cell.model = self.model.data.New_discount[indexPath.item];
        return cell;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionViewOfHot_city) {
        CityViewController *vc = [[CityViewController alloc]init];
        ModelOfCountryHot_city *model = self.model.data.hot_city[indexPath.item];
        vc.ID = [model.ID integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        WebViewOfCommendViewController *vc = [[WebViewOfCommendViewController alloc]init];
        ModelOfCountryNew_discount *model = self.model.data.New_discount[indexPath.item];
        vc.url = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=7.0.1&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6",model.ID];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    return cell;
}
- (void)handleData{
    [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%ld&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&v=1",self.ID] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model = [ModelOfCountry modelWithDic:result];
        
        self.album.arrOfImages = self.model.data.photos;
        self.labelOfEnname.text = self.model.data.enname;
        self.labelOfCnname.text = self.model.data.cnname;
        self.labelOfEntryCont.text = self.model.data.entryCont;
        self.labelOfCnnameButtonOfGuide_num.text = [NSString stringWithFormat:@"%@锦囊",self.model.data.cnname];
        self.labelOfGuide_numButtonOfGuide_num.text = [NSString stringWithFormat:@"全部%ld本",self.model.data.guide_num];
        self.collectionViewOfNew_discount.frame = CGRectMake(0, 505, SCREEN_WIDTH, 110*self.model.data.New_discount.count);
        


        NSLog(@"%f",CGRectGetHeight(self.headView.frame));
        [self.collectionViewOfNew_discount reloadData];
        [self.collectionViewOfHot_city reloadData];
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

}
- (void)goBack:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)goMap{
    [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/map/city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&v=1",self.model.data.ID] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        MapViewController *vc = [[MapViewController alloc]init];
        vc.array = [NSMutableArray array];
        NSArray *array = [result objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            ModelOfCityMap *model = [ModelOfCityMap modelWithDic:dic];
            
            [vc.array addObject:model];
        }
        
        
        [self.navigationController pushViewController:vc animated:YES];
    } andFailureBlock:^(NSError *error) {
        
    }];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
